package com.situ.system.employee.service;

import java.util.List;


import com.situ.system.employee.dao.EmployeeDao;
import com.situ.system.employee.model.EmployeeModel;
import com.situ.system.util.MD5;

public class EmployeeService {

	EmployeeDao dao = new EmployeeDao();

	public String login(EmployeeModel model) {
		EmployeeModel m1 = new EmployeeModel(model.getId());
		List<EmployeeModel> list = dao.select(m1);
		if (list == null || list.isEmpty()) {
			return "0"; // 账号不存在
		}
		EmployeeModel m2 = list.get(0);
		model.setPassword(MD5.encode(model.getPassword()));
		if (m2.getPassword().equals(model.getPassword())) {
			return "1"; // 恭喜你，成功登录
		} else {
			return "2"; // 密码错误
		}
	}

	public String insert(EmployeeModel model) {
		EmployeeModel m1 = new EmployeeModel(model.getId());
		List<EmployeeModel> list = dao.select(m1);
		if (list == null || list.isEmpty()) {
			return dao.insert(model);
		}
		return "-1"; // 已经存在该账号
	}

	public String update(EmployeeModel model) {
		return dao.update(model);
	}

	public String updatepass(EmployeeModel model) {
		return dao.updatepass(model);
	}

	public String updDept(EmployeeModel model) {
		return dao.updDept(model);
	}

	public String delete(EmployeeModel model) {
		return dao.delete(model);
	}

	public List<EmployeeModel> selectAll(EmployeeModel model) {
		List<EmployeeModel> list = dao.select(model);
		return list;
	}

	public List<EmployeeModel> selectList(EmployeeModel model) {
		String employeeCode = model.getId();
		String employeeName = model.getName();
		if (employeeCode == null || employeeCode.trim().isEmpty()) {
			employeeCode = "";
		}
		if (employeeName == null || employeeName.trim().isEmpty()) {
			employeeName = "";
		}
		model.setName("%" + employeeName + "%");
		model.setId("%" + employeeCode + "%");

		List<EmployeeModel> list = dao.select(model);
		return list;
	}

	public Integer selectCount(EmployeeModel model) {
		EmployeeModel model2 = new EmployeeModel();
		String employeeCode = model.getId();
		String employeeName = model.getName();
		if (employeeCode == null || employeeCode.trim().isEmpty()) {
			employeeCode = "";
		}
		if (employeeName == null || employeeName.trim().isEmpty()) {
			employeeName = "";
		}
		model2.setName("%" + employeeName + "%");
		model2.setId("%" + employeeCode + "%");

		return dao.selectCount(model2);
	}

	public EmployeeModel selectModel(EmployeeModel model) {
		List<EmployeeModel> list = selectList(model);
		if (list == null) {
			return null;
		} else {
			return list.get(0);
		}
	}
}
